//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO.Protocol;

import com.goldhuman.Common.Counter;
import com.goldhuman.Common.Marshal.Marshal;
import com.goldhuman.Common.Marshal.MarshalException;
import com.goldhuman.Common.Marshal.OctetsStream;
import com.goldhuman.Common.TimerObserver;
import com.goldhuman.IO.ActiveIO;
import com.goldhuman.IO.PassiveIO;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public abstract class Protocol implements Marshal, Cloneable {
    private static final Map<String, Protocol> map = new HashMap();
    private static final Counter counter = new Counter("Protocol");
    protected int type;
    protected int size_policy;
    protected int prior_policy;
    protected long time_wait = 0L;

    static {
        try {
            com.goldhuman.IO.Protocol.Parser.ParseFlag(map);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        try {
            com.goldhuman.IO.Protocol.Parser.ParseProtocol(map);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        try {
            Parser.ParseRpc(map);
        } catch (Exception var1) {
            var1.printStackTrace();
        }

    }

    public Protocol() {
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static Protocol GetStub(String name) {
        return (Protocol)map.get(name.toUpperCase());
    }

    public static Protocol GetStub(int type) {
        return GetStub(Integer.toString(type));
    }

    public static Protocol Create(String name) {
        Protocol stub = GetStub(name);
        return stub == null ? null : (Protocol)stub.clone();
    }

    public static Protocol Create(int type) {
        return Create(Integer.toString(type));
    }

    public int getType() {
        return this.type;
    }

    protected void Encode(OctetsStream os) {
        os.compact_uint32(this.type).marshal((new OctetsStream()).marshal(this));
    }

    protected static Protocol Decode(com.goldhuman.IO.Protocol.Stream is) throws com.goldhuman.IO.Protocol.ProtocolException {
        if (is.eos()) {
            return null;
        } else {
            Protocol protocol = null;
            int type = 0;
            int size = 0;

            try {
                if (is.check_policy) {
                    is.Begin();
                    type = is.uncompact_uint32();
                    size = is.uncompact_uint32();
                    is.Rollback();
                    if (!is.session.StatePolicy(type) || !is.session.manager.InputPolicy(type, size)) {
                        System.out.println(new Date(System.currentTimeMillis()) + "Protocol Decode CheckPolicy Error:type=" + type + ",size=" + size + ",peer=" + is.session.getPeerAddress());
                        throw new com.goldhuman.IO.Protocol.ProtocolException();
                    }

                    is.check_policy = false;
                    is.checked_size = size;
                }

                com.goldhuman.IO.Protocol.Stream data = new Stream(is.session, is.checked_size);
                is.Begin();
                type = is.uncompact_uint32();
                is.unmarshal(data);
                is.Commit();
                if ((protocol = Create(type)) != null) {
                    protocol.unmarshal(data);
                    counter.increment(protocol.getClass().getName());
                } else {
                    System.out.println("type=" + type + " Not Found");
                }

                is.check_policy = true;
            } catch (MarshalException var5) {
                is.Rollback();
                if (protocol != null) {
                    System.out.println(new Date(System.currentTimeMillis()) + "Protocol Decode Unmarshal Error:type=" + type + ",size=" + size + ",peer=" + is.session.getPeerAddress());
                    throw new com.goldhuman.IO.Protocol.ProtocolException();
                }
            }

            return protocol;
        }
    }

    public static PassiveIO Server(com.goldhuman.IO.Protocol.Manager manager) {
        return PassiveIO.Open(new com.goldhuman.IO.Protocol.Session(manager));
    }

    public static ActiveIO Client(com.goldhuman.IO.Protocol.Manager manager) {
        return ActiveIO.Open(new com.goldhuman.IO.Protocol.Session(manager));
    }

    protected int PriorPolicy() {
        return this.prior_policy;
    }

    protected boolean SizePolicy(int x) {
        return x <= 0 || x < this.size_policy;
    }

    public abstract void Process(Manager var1, Session var2) throws ProtocolException;

    public static void main(String[] argv) {
        TimerObserver.GetInstance().StopTimer();
    }
}
